let resultLoader = require("./zhajinhuaResult").loadFromDocument;
let resultCreator = require("./zhajinhuaResult").create;

function ZhajinhuaGameResult() {
    this.gid = 0;
    this.tid = 0;
    this.code = "";
    this.multiplier = 0;
    this.startTime = 0;
    this.endTime = 0;
    this.maxRounds = 0;
    this.minSeeCardRound = "";
    this.minCompareRound = 0;
    this.results = [];
}

ZhajinhuaGameResult.prototype.toBSON = function() {
    let serializeResults = [];
    for(let result of this.results) {
        serializeResults.push(result.toBSON());
    }
    return {
        gid : this.gid,
        tid: this.tid,
        code : this.code,
        multiplier : this.multiplier,
        startTime: this.startTime,
        endTime : this.endTime,
        maxRounds : this.maxRounds,
        minSeeCardRound: this.minSeeCardRound,
        minCompareRound : this.minCompareRound,
        results : serializeResults
    };
};

ZhajinhuaGameResult.prototype.toJSON = function () {
    let serializeResults = [];
    for(let result of this.results) {
        serializeResults.push(result.toJSON());
    }
    return {
        gid : this.gid,
        tid: this.tid,
        code : this.code,
        multiplier : this.multiplier,
        startTime: this.startTime,
        endTime : this.endTime,
        maxRounds : this.maxRounds,
        minSeeCardRound: this.minSeeCardRound,
        minCompareRound : this.minCompareRound,
        results : serializeResults
    };
};

module.exports.create = function(gid, tid, code, multiplier, startTime, endTime, maxRounds,
                                 minSeeCardRound, minCompareRound, results) {
    let niuniuGameResult = new ZhajinhuaGameResult();
    niuniuGameResult.gid = gid;
    niuniuGameResult.tid = tid;
    niuniuGameResult.code = code;
    niuniuGameResult.multiplier = multiplier;
    niuniuGameResult.startTime = startTime;
    niuniuGameResult.endTime = endTime;
    niuniuGameResult.maxRounds = maxRounds;
    niuniuGameResult.minSeeCardRound = minSeeCardRound;
    niuniuGameResult.minCompareRound = minCompareRound;
    niuniuGameResult.results = [];
    for(let result of results) {
        niuniuGameResult.results.push(
            resultCreator(result.uid, result.cards, result.score, result.cardType, result.blindFlag,
                            result.foldFlag, result.compareLoseFlag, result.compareOpponentUid, result.compareFlag,
                            result.betChips, result.betTimes)
        );
    }
    return niuniuGameResult;
};

module.exports.loadFromDocument = function (document) {
    let gameResult = new ZhajinhuaGameResult();
    gameResult.gid = document["gid"];
    gameResult.tid = document["tid"];
    gameResult.code = document["code"];
    gameResult.multiplier = document["multiplier"];
    gameResult.startTime = document["startTime"];
    gameResult.endTime = document["endTime"];
    gameResult.maxRounds = document["maxRounds"];
    gameResult.minSeeCardRound = document["minSeeCardRound"];
    gameResult.minCompareRound = document["minCompareRound"];
    gameResult.results = [];
    for(let result of document.results) {
        gameResult.results.push(resultLoader(result));
    }
    return gameResult;
};
